package com.probiz.estore.content.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.content.Newsletter;
import com.probiz.estore.common.model.content.NewsletterGroup;
import com.probiz.estore.content.service.NewsletterManager;
import com.probiz.estore.content.service.NewsletterNtManager;
import com.probiz.estore.content.service.SubscriberManager;
import com.probiz.estore.customer.service.CustomerManager;
import com.probiz.estore.system.service.MsgSendManager;
import com.probiz.estore.system.util.MsgTemplateConstants;

public class NewsletterNtManagerImpl implements NewsletterNtManager {
	private CustomerManager customerManager = null;
	
	private NewsletterManager newsletterManager = null;

	private SubscriberManager subscriberManager = null;
	
	private MsgSendManager msgSendManager = null;;

	public void setCustomerManager(CustomerManager customerManager) {
		this.customerManager = customerManager;
	}
	
	public void setSubscriberManager(SubscriberManager subscriberManager) {
		this.subscriberManager = subscriberManager;
	}

	public void setMsgSendManager(MsgSendManager msgSendManager) {
		this.msgSendManager = msgSendManager;
	}

	public void setNewsletterManager(NewsletterManager newsletterManager) {
		this.newsletterManager = newsletterManager;
	}

	public void emailNewsletterJob() {
		int pageNo1=1, pageNo2 =1, pageSize=30;
		//先处理非推向所有客户的订阅
		while(true){
			List<Newsletter> newsletters = newsletterManager.getAllNewslettersToSend(null,pageNo1, pageSize);
			pageNo1++;
			if(newsletters==null || newsletters.size()<=0){
				return;
			}
			for(Newsletter newsletter :newsletters){
				//分批发送消息到前台订阅者
				Map<String, Object> model = new HashMap<String, Object>();
				model.put("newsletter", newsletter);
				try{
					//分批发送消息到所有客户，这里用户非常多，请注意代码质量
					if(newsletter.getToAllCustomers()!=null && newsletter.getToAllCustomers().byteValue()==Constants.FLAG_TRUE.byteValue()){
						pageNo2=1;
						while(true){
							List<String> emails = customerManager.searchCustomerEmails(pageNo2, pageSize, null);
							if(emails!=null && emails.size()>0){
								msgSendManager.sendMsg(MsgTemplateConstants.MESSAGE_INFORMATION_TAKE, model, emails.toArray(new String[emails.size()]), null, null );
								pageNo2++;
							}else
								break;
						}
					}else{//非推送到网店所有客户
						pageNo2=1;
						java.util.Set<NewsletterGroup> newsletterGroups = newsletter.getNewsletterGroups();
						List<String> newsletterGroupIds = new ArrayList<String>();
						for(NewsletterGroup ng: newsletterGroups){
							newsletterGroupIds.add(ng.getNewsletterGroupId().toString());
						}
						while(true){
							List<String> emails = subscriberManager.searchEmails(pageNo2, pageSize, newsletterGroupIds);
							if(emails!=null && emails.size()>0){
								msgSendManager.sendMsg(MsgTemplateConstants.MESSAGE_INFORMATION_TAKE, model, emails.toArray(new String[emails.size()]), null, null);
								pageNo2++;
							}else
								break;
						}
					}
					
					//置为已发送
					newsletter.setStatus(Constants.FLAG_TRUE);
					newsletterManager.save(newsletter);
					newsletterManager.flush();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}
}
